﻿var GroupDetail = function () {
    var initMemberListPanel = function () {
        var ddlMemberList = $("#ddlMemberList");

        ddlMemberList.multiSelect();
    };

    // 初始化更新组信息面板
    var initUpdateInfoPanel = function () {
        $("#updateForm")
        .AjaxForm({
            submitButton: {
                selector: "#btnUpdateGroup",
                loadingText: "正在更新"
            },
            resetButton: {
                selector: "#btnUpdateGroupCancel"
            },
            alertTitle: "更新分组信息",
            notCompletedResetForm: false,
            success: function (response, title) {
                var args = [];
                args.push(response.Message);
                if (title) args.push(title);

                if (response.Result) {
                    toastr["success"].apply(this, args);
                    var tr = $("#tr" + response.Data.GID);
                    tr.children("[data-field]").each(function () {
                        var td = $(this);
                        switch (td.attr("data-field")) {
                            case "groupname":
                                td.html(response.Data.GroupName);
                                break;
                            case "descr":
                                td.html(response.Data.Descr);
                                break;
                        }
                    });
                } else {
                    toastr["error"].apply(this, args);
                }
            }
        })
        .validate({
            onsubmit: false,
            rules: {
                groupname: {
                    required: true,
                    maxlength: 10
                },
                descr: {
                    maxlength: 30
                }
            },
            messages: {
                groupname: {
                    required: "分组名称必填",
                    maxlength: "字数不能超过10个"
                },
                descr: {
                    maxlength: "字数不能超过30个"
                }
            },
            errorPlacement: function (error, element) {
                error.removeClass().addClass("text-danger ml-20");
                var _container = element.prev();
                _container.empty();
                error.appendTo(_container);
                element.parent().removeClass("has-success").addClass("has-error");
            },
            success: function (label, element) {
                label.empty().append('<i class="fa fa-check"></i>');
                label.removeClass().addClass("text-success ml-20");
                $(element).parent().removeClass("has-error").addClass("has-success");
            }
        });
    };

    var initMemberShipPanel = function () {
        $("#changeMemberForm").AjaxForm({
            submitButton: {
                selector: "#btnChangeMember",
                loadingText: "正在更新"
            },
            resetButton: {
                selector: "#btnCancelChange"
            },
            alertTitle: "更新成员列表",
            notCompletedResetForm: false,
            success: function (response, title) {
                var args = [];
                args.push(response.Message);
                if (title) args.push(title);

                if (response.Result) {
                    toastr["success"].apply(this, args);
                    var tr = $("#tr" + response.Data.GID);
                    tr.children("[data-field]").each(function () {
                        var td = $(this);
                        if (td.attr("data-field") == "membercount") {
                            td.html(response.Data.Count);
                        }
                    });
                } else {
                    toastr["error"].apply(this, args);
                }
            }
        });
    };

    var initUpdatePermissionPanel = function () {
        $("#updatePermissionForm")
        .AjaxForm({
            submitButton: {
                selector: "#btnUpdatePermission",
                loadingText: "正在更新"
            },
            resetButton: {
                selector: "#btnCancelUpdatePermission"
            },
            alertTitle: "更新组权限",
            notCompletedResetForm: false
        });
    };

    var initUpdateMenuPermissionPanel = function () {
        $("#updateMenuSettingForm")
        .AjaxForm({
            submitButton: {
                selector: "#btnUpdateMenuPermission",
                loadingText: "正在更新"
            },
            resetButton: {
                selector: "#btnCancelUpdateMenuPermission"
            },
            alertTitle: "更新组权限",
            notCompletedResetForm: false
        });
    };

    return {
        init: function () {
            initMemberListPanel();
            initUpdateInfoPanel();
            initMemberShipPanel();
            initUpdatePermissionPanel();
            initUpdateMenuPermissionPanel();
            Metronic.initComponents();
        }
    }
} ();